package xm.chengdu.com.beijing_211;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.github.czy1121.view.RoundButton;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;
import top.defaults.view.DateTimePickerView;
import com.sdwfqin.cbt.Content;
import utils.FaSong;

public class TimeActivity extends Activity {

    @BindView(R.id.img_back)
    RelativeLayout imgBack;
    @BindView(R.id.textView4)
    TextView textView4;
    @BindView(R.id.datePickerView)
    DateTimePickerView dateTimePickerView;
    @BindView(R.id.Email)
    TextView Email;
    @BindView(R.id.home)
    RoundButton home;
    String time;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_time);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);

        dateTimePickerView.setStartDate(Calendar.getInstance());
        dateTimePickerView.setType(2);
// 注意：月份是从0开始计数的
        dateTimePickerView.setSelectedDate(new GregorianCalendar(2018, 9, 15, 21, 30));
        dateTimePickerView.setOnSelectedDateChangedListener(new DateTimePickerView.OnSelectedDateChangedListener() {
            @Override
            public void onSelectedDateChanged(Calendar date) {
                String  monthS=null;
                String  dayOfMonths=null;
                String  WEEKs=null;
                String  hours=null;
                String  minutes=null;

                int year = date.get(Calendar.YEAR);
                int month = date.get(Calendar.MONTH);
                int dayOfMonth = date.get(Calendar.DAY_OF_MONTH);
                int hour = date.get(Calendar.HOUR_OF_DAY);
                int minute = date.get(Calendar.MINUTE);
                int   sss= date.get(Calendar.DAY_OF_WEEK);
                Log.e("周几", ""+(sss-1));
                int WEEK = date.get(Calendar.DAY_OF_WEEK)-1;


                String dateString = String.format(Locale.getDefault(), "%d/%02d/%02d  %02d:%02d", year, month + 1, dayOfMonth, hour, minute);
                if (String.valueOf(month+1).length()==1) {
                    monthS="0"+(month+1);
                } else {
                    monthS=""+(month+1);
                }
                if (String.valueOf(dayOfMonth).length()==1) {
                    dayOfMonths="0"+dayOfMonth;
                } else {
                    dayOfMonths=""+dayOfMonth;
                }
                if (String.valueOf(WEEK).length()==1) {
                    if (String.valueOf(WEEK).equals("0")) {
                        WEEKs="07" ;
                    }else {
                        WEEKs="0"+WEEK;
                    }

                } else {
                    if (String.valueOf(WEEK).equals("0")) {
                        WEEKs=""+7;
                    }else {
                        WEEKs=""+WEEK;
                    }

                }
                if (String.valueOf(hour).length()==1) {
                    hours="0"+hour;
                } else {
                    hours=""+hour;
                }
                if (String.valueOf(minute).length()==1) {
                    minutes="0"+minute;
                } else {
                    minutes=""+minute;
                }
                 time=""+year+monthS+dayOfMonths+WEEKs+hours+minutes+"00";
                Email.setText(dateString);
                Log.e("time", time);

            }
        });

    }
    public void onEventMainThread(MyEvent event) {

        if (!TextUtils.isEmpty(event.getType())) {
            if (event.getType().equals("settingsuccess")) {
                finish();
            }else if (event.getType().equals("settingerr")) {
                finish();
            }


        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @OnClick({R.id.img_back, R.id.home})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.home:

                if (!Content.isConn) {
                    Toast.makeText(this, "Please connect to Bluetooth first.", Toast.LENGTH_SHORT).show();
                    return;
                }


                String et_shijians = Email.getText().toString();
                if (TextUtils.isEmpty(et_shijians)||TextUtils.isEmpty(time) ) {
                    Toast.makeText(this, "Please input the information sent", Toast.LENGTH_SHORT).show();
                    return;
                }

                et_shijians = FaSong.shezhishijian(time);
                MyEvent eventet_shijians = new MyEvent();
                eventet_shijians.setType("message");
                eventet_shijians.setContent(et_shijians);
                EventBus.getDefault().post(eventet_shijians);
//                Toast.makeText(this, "success！", Toast.LENGTH_SHORT).show();

                break;
        }
    }



}
